package com.resou.reader.mine.vipcenter.monthlybooks;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.ActivityRechargePrice;
import com.resou.reader.api.entry.FilterBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.ResultList;
import com.resou.reader.api.service.VipService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.bookstore.model.BasicList;
import com.resou.reader.data.bookstore.model.PlateNovels;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 包月书库Presenter层
 */
public class MonthlyBooksPresenter extends ResouBasePresenter<IMonthlyBooksContract.IMonthlyBooksView> implements IMonthlyBooksContract.IMonthlyBooksPresenter {
    VipService vipService;

    public MonthlyBooksPresenter(IMonthlyBooksContract.IMonthlyBooksView view) {
        super(view);
        vipService = ApiImp.getInstance().getService(VipService.class);
    }


    /***
     *
     * @param cate    vip筛选条件返回值filterType为1的筛选条件所选值，全部时传入-1
     * @param word    vip筛选条件返回值filterType为2的筛选条件所选值，全部时传入-1
     * @param serial  vip筛选条件返回值filterType为3的筛选条件所选值，全部时传入-1
     * @param channel 男生小说传1  女生小说传2  默认为1
     * @param page    页码，默认为0，从0开始
     */
    @Override
    public void quearBookList(int cate, int word, int serial, int channel, int page) {
        if (page == 0) {
            mView.getSmartRefreshLayout().setNoMoreData(false);
            mView.showProgress();
        }
        vipService.getVIPChannel(cate, word, serial, channel, page, pageSize)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<BasicList<PlateNovels>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<BasicList<PlateNovels>> plateNovelsBasicList) {
                        if (plateNovelsBasicList.getData().getBooks().size() < 15) {
                            if (page == 0) {
                                mView.getSmartRefreshLayout().finishRefresh();
                                if (plateNovelsBasicList.getData().getBooks().size() == 0) {
                                    mView.showBookList(null);
                                    mView.showEmpty();
                                    return;
                                }
                            }
                            mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                            mView.showBookList(plateNovelsBasicList.getData().getBooks());
                        } else {
                            if (page == 0) {
                                mView.getSmartRefreshLayout().finishRefresh();
                            }
                            mView.getSmartRefreshLayout().finishLoadMore();
                            mView.showBookList(plateNovelsBasicList.getData().getBooks());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.getSmartRefreshLayout().finishRefresh();
                        mView.getSmartRefreshLayout().finishLoadMore();
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });

    }

    /***
     *获取赛选标签数据
     * @param channel 男生：1；女生：2
     */
    @Override
    public void quearFilter(int channel) {
        vipService.getVIPFilter(channel)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Consumer<ResultList<FilterBean>>() {
                    @Override
                    public void accept(ResultList<FilterBean> filterBeanResult) throws Exception {
                        mView.showFilter(filterBeanResult.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showError(throwable);
                    }
                });
    }

}
